import Dialog from '../../../dist/dialog/dialog';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:1,
    couponSn:'',
    buyNotice:[],
    packageDetail:[],
    noticeDesc:'',
    priceDesc: '',
    storeId:null,
    offlinePrice:0,
    orderGoods:{},
    pluginSetting:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.storeId??'') !== '') {
      this.setData({
        storeId: parseInt(options.storeId)
      })
    }
    if((options.couponSn??'') !== '') {
      this.setData({
        couponSn: options.couponSn
      })
    }
    this.storeGoodsList();
  },
  onRefund(event) {
    const id = event.currentTarget.dataset.id;
    if(id) {
      Dialog.confirm({
        title: '提示',
        message: `是否确认核销消费券号：${this.data.couponSn}`,
      })
        .then(() => {
          let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.examineCoupon");
          let json = {
            cstore_id:this.data.storeId,
            coupon_sn: this.data.couponSn,
            request_type:2
          };
          app._postNetWork({
            url: urlStr,
            data:json,
            success: function (resdata) {
              let res = resdata.data;
              if (res.result == 1) {
                console.log(res);
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });

              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              },1000)
            },
            fail: function (res) {
              console.log(res);
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              },1000)
            }
          });
        })
        .catch(() => {
          // on cancel
        });
    } else {
      Dialog.alert({
        message: '不是适用门店暂时不能核销',
      })
        .then(() => {
          // on confirm
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          },1000)
        })
    }

    
  },
  storeGoodsList() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.examineCoupon");
    let json = {
      cstore_id:this.data.storeId,
      coupon_sn: this.data.couponSn,
      request_type:1
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            orderGoods: res.data.order_goods,
            offlinePrice: res.data.offline_price,
            buyNotice: res.data.buy_notice,
            packageDetail: res.data.package_detail,
            noticeDesc: res.data.notice_desc,
            priceDesc: res.data.price_desc,
            pluginSetting: res.basic_info?.plugin_setting?.coupon_store
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          },1000)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})